﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity;
using DAL;
using Common;
using System.Data.Linq;
namespace BAL
{
    public class QuesType
    {
        private static string CacheName = "AllQuesType";
        internal static QuesTypeInfo TransQuesType2QuesTypeInfo(Models.QuesType questype)
        {
            QuesTypeInfo quesTypeInfo = new QuesTypeInfo();

            Utility.SetObjectValueWithTheSamePropertyName(quesTypeInfo, questype);

            return quesTypeInfo;
        }

        public static QuesTypeInfo AddQuesType(QuesTypeInfo quesTypeInfo)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                Models.QuesType questype = new Models.QuesType();
                Utility.SetObjectValueWithTheSamePropertyName(questype, quesTypeInfo);
                DatabaseHelper.Insert<Models.QuesType>(questype);
                ToCache();
                return TransQuesType2QuesTypeInfo(questype);
            }
        }

        public static void EditQuesType(QuesTypeInfo quesTypeInfo)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                try
                {
                    Models.QuesType questype = db.QuesTypes.Single(b => b.Id == quesTypeInfo.Id);

                    Utility.SetObjectValueWithTheSamePropertyName(questype, quesTypeInfo);

                    db.SubmitChanges(ConflictMode.ContinueOnConflict);
                    ToCache();
                }
                catch (ChangeConflictException)
                {
                    foreach (ObjectChangeConflict occ in db.ChangeConflicts)
                    {
                        occ.Resolve(RefreshMode.KeepCurrentValues);
                    }
                }
            }
        }
        public void Update(int id, string name)
        {

        }
        public void Delete(int id)
        {

        }
        public static void DelQuesType(int id)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                DelQuesType(new int[] { id });
                ToCache();
            }
        }

        public static void DelQuesType(int[] lstId)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                if (lstId.Length > 0)
                {
                    foreach (int id in lstId)
                    {
                        Question.DelQuestionByQuesType(id);
                    }
                    string sql = string.Format("delete from QuesType where id in ({0})", string.Join(",", lstId.ConvertToStringArray()));
                    db.ExecuteCommand(sql);
 
                }
                ToCache();
            }
        }
        /// <summary>
        /// 写入缓存
        /// </summary>
        /// <returns></returns>
        public static void ToCache()
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                CacheHelper.Refurbish(CacheName, db.QuesTypes.ToList(), 0, 60);
            }
        }

        public static int GetQuesTypeIdByKeyword(string keyword)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.QuesType> QuesType_Info = (IList<Models.QuesType>)CacheHelper.GetCache(CacheName);
            return QuesType_Info.Single(t => t.Keyword == keyword).Id;
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    return db.QuesTypes.Single(t => t.Keyword == keyword).Id;
            //}
        }

        public static QuesTypeInfo GetQuesTypeInfoById(int Id)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.QuesType> QuesType_Info = (IList<Models.QuesType>)CacheHelper.GetCache(CacheName);
            return TransQuesType2QuesTypeInfo(QuesType_Info.Single(t => t.Id == Id));
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    Models.QuesType questype = db.QuesTypes.Single(b => b.Id == Id);
            //    return TransQuesType2QuesTypeInfo(questype);
            //}
        }
        public static IList<QuesTypeInfo> GetQuesTypeInfos(int first, int max)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.QuesType> QuesType_Info = (IList<Models.QuesType>)CacheHelper.GetCache(CacheName);
            if (max == 0)
            {
                return QuesType_Info.Select(c => TransQuesType2QuesTypeInfo(c)).ToList();
            }
            else
            {
                return QuesType_Info.Skip(first).Take(max).Select(c => TransQuesType2QuesTypeInfo(c)).ToList();
            }
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    if (max == 0)
            //    {
            //        return db.GetTable<Models.QuesType>().Select(c => TransQuesType2QuesTypeInfo(c)).ToList();
            //    }
            //    else
            //    {
            //        return db.GetTable<Models.QuesType>().Skip(first).Take(max).Select(c => TransQuesType2QuesTypeInfo(c)).ToList();
            //    }
            //}
        }

        public static int GetQuesTypeInfoCount()
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.QuesType> QuesType_Info = (IList<Models.QuesType>)CacheHelper.GetCache(CacheName);
            return QuesType_Info.Count();
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    return db.GetTable<Models.QuesType>().Count();
            //}
        }
        public static bool CheckQuesTypeExistById(int Id)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.QuesType> QuesType_Info = (IList<Models.QuesType>)CacheHelper.GetCache(CacheName);
            return QuesType_Info.Where(t => t.Id == Id).Count() > 0;
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    return db.QuesTypes.Where(w => w.Id == Id).Count() > 0;
            //}
        }

        public static bool CheckQuesTypeExistByKeyword(string keyword, int editid)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.QuesType> QuesType_Info = (IList<Models.QuesType>)CacheHelper.GetCache(CacheName);
            return QuesType_Info.Where(t => t.Keyword == keyword && t.Id != editid).Count() > 0;
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    return db.QuesTypes.Where(t => t.Keyword == keyword && t.Id != editid).Count() > 0;
            //}
        }

        public static bool CheckQuesTypeExistByName(string name)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.QuesType> QuesType_Info = (IList<Models.QuesType>)CacheHelper.GetCache(CacheName);
            return QuesType_Info.Where(t => t.Name == name).Count() > 0;
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    return db.QuesTypes.Where(w => w.Name == name).Count() > 0;
            //}
        }

        public static bool CheckQuesTypeExistByName(string name,string keyword)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.QuesType> QuesType_Info = (IList<Models.QuesType>)CacheHelper.GetCache(CacheName);
            return QuesType_Info.Where(w => w.Name == name || w.Keyword == keyword).Count() > 0;
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    return db.QuesTypes.Where(w => w.Name == name || w.Keyword==keyword).Count() > 0;
            //}
        }
    }
}
